package com.puman.watchtrade.util;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.puman.watchtrade.fragment.set.model.UserInfo;

public class DBAdapter {
	private static final String DB_NAME = "user.db";
	private static final String DB_TABLE = "userinfo";
	private static final int DB_VERSION = 1;
	
	public static final String KEY_ID = "_id";
	public static final String KEY_ACCOUNT = "account";
	public static final String KEY_PASSWORD = "password";
	public static final String KEY_NAME = "realname";
	
	private SQLiteDatabase db;
	private final Context context;
	private DBOpenHelper dbOpenHelper;
	private static class DBOpenHelper extends SQLiteOpenHelper {
		public DBOpenHelper(Context context, String account, CursorFactory factory, int version){
			   super(context, account, factory, version);
	}
		private static final String DB_CREATE = "create table " + 
			    DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +
			    KEY_ACCOUNT+ " text not null, "+
			    KEY_PASSWORD+ " text not null, " + KEY_NAME+ " text not null);" ;
		    @Override
		     public void onCreate(SQLiteDatabase _db) {
			      _db.execSQL(DB_CREATE);
			      }
		    
		    @Override
			public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
				_db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
				onCreate(_db);
			}
		}

	public DBAdapter(Context _context) {
	        context = _context;
	}
 
	public void open() throws SQLiteException {
	        dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
	        try {
			db = dbOpenHelper.getWritableDatabase();
	         }catch (SQLiteException ex) {
			db = dbOpenHelper.getReadableDatabase();
	          }
	}
	public void close() {
        if (db != null){
		db.close();
		db = null;
         }
}
	////////��ӹ���//////////
	public long insert(UserInfo userInfo) {
		ContentValues newValues = new ContentValues();
		
//		newValues.put(KEY_ACCOUNT, userInfo.account);
//		newValues.put(KEY_PASSWORD, userInfo.password);
//		newValues.put(KEY_NAME, userInfo.realName);
		
		return db.insert(DB_TABLE, null, newValues);
	}
	
	////////////////ɾ����////////////
	public long deleteAllData() {
		return db.delete(DB_TABLE, null, null);
	}
	public long deleteOneData(long id) {
		return db.delete(DB_TABLE,  KEY_ID + "=" + id, null);
	}

	////////////////���¹���//////////
	public long updateOneData(long id , UserInfo userInfo){
		ContentValues updateValues = new ContentValues();	  
//		updateValues.put(KEY_ACCOUNT, userInfo.account);
//		updateValues.put(KEY_PASSWORD, userInfo.password);
//		updateValues.put(KEY_NAME, userInfo.realName);
		return db.update(DB_TABLE, updateValues,  KEY_ID + "=" + id, null);
	}

	////////////////��ѯ����/////////////
	private UserInfo[] ConvertToPeople(Cursor cursor){
		int resultCounts = cursor.getCount();
		if (resultCounts == 0 || !cursor.moveToFirst()){
		            return null;
		}
		UserInfo[] users = new UserInfo[resultCounts];
		for (int i = 0 ; i<resultCounts; i++){
			users[i] = new UserInfo();
//			users[i].ID = cursor.getInt(0);
//			users[i].account = cursor.getString(cursor.getColumnIndex(KEY_ACCOUNT));
//			users[i].password = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD));
//			users[i].realName = cursor.getString(cursor.getColumnIndex(KEY_NAME));
		            cursor.moveToNext();
		}
		return users; 
	}
	
	///////////////���ID��ѯ////////
	public UserInfo[] getOneData(long id) {
		Cursor results =  db.query(DB_TABLE, new String[] { KEY_ID, KEY_ACCOUNT, KEY_PASSWORD,KEY_NAME}, KEY_ID + "=" + id, null, null, null, null);
		return ConvertToPeople(results);
	}

	////////////���ID��ѯȫ��/////////
	public UserInfo[] getAllData() {  
		
		Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_ACCOUNT, KEY_PASSWORD, KEY_NAME}, null, null, null, null, null);
		if(results.getCount()!=0){
		return ConvertToPeople(results);
		}else
			return null;
	}
}

